package com.superiron.scaffold.security.detail;

import com.superiron.scaffold.entity.MenuEntity;
import com.superiron.scaffold.entity.UserEntity;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表
 * @author yzg
 * @date 2022年6月20日10:16:11
 * 版权所有 泰山信息科技有限公司
 */
@Data
public class IotGlobalUser implements UserDetails {
	
	private UserEntity userEntity;
	//拥有资源列表
	private List<IotGlobalRole> menuList;
	
	public IotGlobalUser(UserEntity userEntity, List<IotGlobalRole> menuList) {
		this.userEntity = userEntity;
		this.menuList = menuList;
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//返回当前用户的角色
		return menuList.stream()
				.map(role ->new SimpleGrantedAuthority(role.getCode()))
				.collect(Collectors.toList());
	}
	
	@Override
	public String getPassword() {
		return userEntity.getPassword();
	}
	
	@Override
	public String getUsername() {
		return userEntity.getMobile();
	}
	
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
	
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}
	
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	@Override
	public boolean isEnabled() {
		return userEntity.getStatus();
	}
}
